import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'


Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:"/login"
  },
  {
    path:'/login',
    name:'login',
    component:()=>import("@/views/LoginView.vue")
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children:[
      {
        path:"/",
        redirect:"/home/add"
      },
      {
          path:"message",
          component:()=>import("@/views/MessageList.vue")
      },
      {
        path:"add",
        component:()=>import("@/views/MessageAdd.vue")
      },
      {
        path:"user",
        component:()=>import("@/views/UserList.vue")
      },
      {
        path:"adduser",
        component:()=>import("@/views/AddUser.vue")
      }
    ]
  },
  {
    path: '/register',
    name: 'register',
    component: ()=>import("@/views/RegisterView.vue")
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  let token = localStorage.token
  if(to.path=='/login'){
    next()
  }else if(!token){
    next('/login')
  }else{
    next()
  }
})

export default router
